let jsonPath = '/json/header.json';

refresh = function (){
    const headTd = document.getElementById('headTd');
    const hostTd = document.getElementById('hostTd');
    getRule(
        function (){
            getJson(jsonPath,function (data){
                headTd.innerHTML = JSON.stringify(data[0].action.requestHeaders);
                hostTd.innerHTML = data[0].condition.urlFilter;
            });
        },
        function (jsonData){
            headTd.innerHTML = JSON.stringify(jsonData[0].action.requestHeaders);
            hostTd.innerHTML = jsonData[0].condition.urlFilter;
        }
    );
}

getJson = function (path,callback){
    const xhr = new XMLHttpRequest();
    xhr.open('GET', path);
    xhr.send(null);
    xhr.onload = function (){
        let data = JSON.parse(xhr.responseText);
        callback(data);
    }
}

getRule = function (callbackIf,callbackElse){
    chrome.declarativeNetRequest.getDynamicRules(rules=>{
        let jsonData = JSON.parse(JSON.stringify(rules));
        if(jsonData.length === 0){
            callbackIf();
        }else{
            callbackElse(jsonData);
        }
    })
}

document.addEventListener('DOMContentLoaded', function() {
    // 获取需要绑定事件的元素
    const editHeadButton = document.getElementById('editHead');
    const editHostButton = document.getElementById('editHost');
    const reSetButton = document.getElementById('reSet');

    // 绑定事件处理器
    editHeadButton.addEventListener('click', function() {
        const headTd = document.getElementById('headTd');
        let editHead = prompt("修改值：",headTd.innerHTML)
        if(editHead){
            getRule(
                function (){
                    getJson(jsonPath,function (data){
                        data[0].action.requestHeaders = JSON.parse(editHead);
                        chrome.runtime.sendMessage({"type":"editHeadAgent","data":data});
                    })
                },
                function (data){
                    data[0].action.requestHeaders = JSON.parse(editHead);
                    chrome.runtime.sendMessage({"type":"editHeadAgent","data":data});
                }
            )
            location.reload();
        }
    });
    // 绑定事件处理器
    editHostButton.addEventListener('click', function() {
        const hostTd = document.getElementById('hostTd');
        let editHost = prompt("修改值：",hostTd.innerHTML)
        if(editHost){
            getRule(
                function (){
                    getJson(jsonPath,function (data){
                        data[0].condition.urlFilter = editHost;
                        chrome.runtime.sendMessage({"type":"editHostAgent","data":data});
                    })
                },
                function (data){
                    data[0].condition.urlFilter = editHost;
                    chrome.runtime.sendMessage({"type":"editHostAgent","data":data});
                }
            )
            location.reload();
        }
    });
    //重置按钮事件
    reSetButton.addEventListener('click',function (){
        let confirmValue = confirm("重置会初始化 head 设置及 host 设置。Are you sure?");
        if(confirmValue){
            getJson(jsonPath,function (data){
                chrome.runtime.sendMessage({"type":"reSet","data":data});
                location.reload();
            })
        }
    })
});

window.onload=function (){
    refresh()
}